using System;
using System.Collections.Generic;
using System.IO;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class CoroutineMgr : MonoBehaviour
{
    private static CoroutineMgr m_Instance;
    public static CoroutineMgr Instance
    {
        get
        {
            if (m_Instance == null)
            {
                var go = new GameObject("CoroutineMgr");
                m_Instance = go.AddComponent<CoroutineMgr>();
                DontDestroyOnLoad(go);
            }
            return m_Instance;
        }
    }
}

public class TestHmNetWork : MonoBehaviour
{
    public TMP_InputField InputMaxNum;
    public Button BtnClearCache;
    public Button BtnTestHttpWebRequest;
    public Button BtnTestHmBackground;
    public Button BtnTestAndroidBackgournd;

    private void Start()
    {
        InputMaxNum.onValueChanged.AddListener(OnValueChange);
        BtnClearCache.onClick.AddListener(ClearCache);
        BtnTestHttpWebRequest.onClick.AddListener(DownloadByUnityWebRequest);
        BtnTestHmBackground.onClick.AddListener(DownloadByHmNet);
        BtnTestAndroidBackgournd.onClick.AddListener(DownloadByAndroid);
    }

    void OnValueChange(string numStr)
    {
        if (Int32.TryParse(numStr, out var num))
        {
            NetWorkDefine.s_MaxDownloadingCount = num;
        }
    }

    void ClearCache()
    {
        Directory.Delete(Application.persistentDataPath, true);
        Debug.Log($"Deleted directory: {Application.persistentDataPath}");
    }

    void DownloadByUnityWebRequest()
    {
        Queue<BackGroundDownloadParam> downloadParamsQueue = new Queue<BackGroundDownloadParam>();
        foreach (var relativePath in TestUrls.k_RelativePath)
        {
            downloadParamsQueue.Enqueue(new BackGroundDownloadParam(relativePath, relativePath, 1024));
        }
        var queue = new HttpWebRequestDownloadQueue(downloadParamsQueue);
        queue.StartDownload();
    }

    void DownloadByHmNet()
    {
        List<BackGroundDownloadParam> list = new List<BackGroundDownloadParam>();
        foreach (var relativePath in TestUrls.k_RelativePath)
        {
            var item = new BackGroundDownloadParam(relativePath, relativePath, 1024);
            list.Add(item);
        }
        SGBackGroundDownloadMgr.Instance.GetDownloader().SetDownloadParam(NetWorkDefine.s_MaxDownloadingCount, 1, 2);
        SGBackGroundDownloadMgr.Instance.GetDownloader().AddDownload(list);
        SGBackGroundDownloadMgr.Instance.GetDownloader().StartDownload();
    }
    
    void DownloadByAndroid()
    {
        List<BackGroundDownloadParam> list = new List<BackGroundDownloadParam>();
        foreach (var relativePath in TestUrls.k_RelativePath)
        {
            var item = new BackGroundDownloadParam(relativePath, relativePath, 1024);
            list.Add(item);
        }
        SGBackGroundDownloadMgr.Instance.GetDownloader(BackGroundDownloadType.Android).SetDownloadParam(NetWorkDefine.s_MaxDownloadingCount, 1, 2);
        SGBackGroundDownloadMgr.Instance.GetDownloader(BackGroundDownloadType.Android).AddDownload(list);
        SGBackGroundDownloadMgr.Instance.GetDownloader(BackGroundDownloadType.Android).StartDownload();
    }

}